
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

import lejos.pc.comm.NXTConnector;



public class TermProjectHost {

	 static String content = "Here men from the planet Earth\n"
		      + "first set foot upon the Moon,\n" + "July 1969, AD.\n"
		      + "We came in peace for all mankind.";
			
	static InputStream is;
	static DataInputStream str;
			
	static OutputStream os;
	static DataOutputStream stros;
	
	public static void main(String[] args) throws IOException, InterruptedException {
		
		String[] movementTypes = {"FOLLOW_LEFT_WALL", "FOLLOW_RIGHT_WALL",
				"EXPLORE_RIGHT", "EXPLORE_LEFT", "MOVE_TO_RAMP_FIRST_FLOOR",
				"CLIMB_RAMP","EXPLORE_SECOND_FLOOR"," FIND_PRINCESS", "MOVE_TO_RAMP_SECOND_FLOOR",
				"CLIMB_RAMP_DOWN"," RETURN_PRINCESS_START_AREA"," RETURN_PRINCESS_TO_KING"};
		
		// TODO Auto-generated method stub
		
		JFrame myFirstFloorFrame = new JFrame();
		myFirstFloorFrame.setTitle("First Floor");
		myFirstFloorFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		myFirstFloorFrame.setSize(4 * 60,11 * 60);
		MyGridWindow myFirstFloorGrid = new MyGridWindow(11,4);
		myFirstFloorFrame.add(myFirstFloorGrid);
		myFirstFloorFrame.setVisible(true);
		
		
		JFrame mySecondFloorFrame = new JFrame();
		mySecondFloorFrame.setTitle("Second Floor");
		mySecondFloorFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		mySecondFloorFrame.setSize(4 * 60,3 * 60);
		MyGridWindow mySecondFloorGrid = new MyGridWindow(3,4);
		mySecondFloorFrame.add(mySecondFloorGrid);
		mySecondFloorFrame.setVisible(true);
		
		JFrame f = new JFrame("Text Area Examples");
		JPanel TextPanel = new JPanel();
		f.add(TextPanel);
		
		JTextArea myTextArea = new JTextArea(10, 60);
		myTextArea.setLineWrap(true);
		TextPanel.add(new JScrollPane(myTextArea));

	    f.pack();
	    f.setVisible(true);
	  
	    myTextArea.append("Lorem Ipsum...\n");
	    
	    NXTConnector conn = new NXTConnector();

        boolean connected = conn.connectTo("btspp://" );

        if ( connected ) 
        {
            System.out.println("Connected!");
        }
        else
        {
            System.out.println("Could not connect to any NXTs");
            System.exit(1);
        }

        os = conn.getOutputStream();
        stros = new DataOutputStream(os);
        
        is = conn.getInputStream();
		str = new DataInputStream( is );
		
		while( true )
		{	
			int movementType = str.readInt();
			int readColor = str.readInt();
			int direction = str.readInt();
			int x = str.readInt();
			int y = str.readInt();
			
			System.out.println("Movement Type: " + movementTypes[movementType] + " Color: " + readColor + " Direction: " + direction + " x:  " + x + " y: " + y);
			
			if(movementType <= 4) {
				myFirstFloorGrid.paintCell(y, x, readColor);
			} else if (movementType == 7) {
				mySecondFloorGrid.paintCellLevel2(y,x,readColor);
			}
			
			myTextArea.append("Movement Type: " + movementTypes[movementType] + " Color: " + readColor + " Direction: " + direction + " x:  " + x + " y: " + y + "\n");
		}
		
		
	}

}
